package work.service.impl;

import work.dao.UserDao;
import work.pojo.User;
import work.service.UserService;

import java.util.Objects;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    private UserDao dao;

    public void setUserDao(UserDao userDao) {
        this.dao = userDao;
    }

    @Override
    public boolean exist(String name) {
        User user = dao.findByUsername(name);
        System.out.println(user);
        // user不等于null，则返回true
        return Objects.nonNull(user);
    }

    @Override
    public boolean register(User user) {
        if (dao.findByUsername(user.getUsername()) != null) {
            return false;
        }
        String code = UUID.randomUUID().toString().replace("-", "");
        user.setCode(code);
        dao.add(user);
        // sqlSession.commit();
        return true;
    }

    @Override
    public User login(String username, String password) {
        return dao.findByUsernameAndPassword(username, password);
    }

    @Override
    public boolean active(String code) {
        User user = dao.findByCode(code);
        if (user != null) {
            if ("0".equals(user.getStatus())) {
                dao.updateStatus(user.getId());
                // sqlSession.commit();
            }
            return true;
        }
        return false;
    }
}
